//
//  XHTabBarController.m
//  BSNoAnswer
//
//  Created by Tiffany宝 on 16/5/12.
//  Copyright © 2016年 Tiffany. All rights reserved.
//

#import "XHTabBarController.h"
#import "XHEssenceViewController.h"
#import "XHNewViewController.h"
#import "XHFriendTrendsViewController.h"
#import "XHMeViewController.h"
#import "XHTabBar.h"

@interface XHTabBarController ()

@end

@implementation XHTabBarController

//在 initialize 方法中只执行一次
+(void)initialize{
    // 统一给所有的 UITabBarItem 设置文字属性
    // 只有后面带有 UI_APPEARANCE_SELECTOR 的属性或方法, 才可以通过appearance对象来统一设置
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor],NSFontAttributeName:[UIFont systemFontOfSize:12]} forState:UIControlStateNormal];
    [item setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]} forState:UIControlStateSelected];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupChildVc:[[XHEssenceViewController alloc]init] Title:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];
    [self setupChildVc:[[XHNewViewController alloc]init] Title:@"最新" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];
    [self setupChildVc:[[XHFriendTrendsViewController alloc]init] Title:@"关注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];
    [self setupChildVc:[[XHMeViewController alloc]init] Title:@"我" image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon"];
    
    XHTabBar *tabBar = [[XHTabBar alloc]init];
//    self.tabBar = tabBar;
    //使用 KVC 进行自定义 tabbar
    [self setValue:tabBar forKey:@"tabBar"];
    
    
//    UIViewController *Vc = [[UIViewController alloc]init];
//    Vc.view.backgroundColor = [UIColor orangeColor];
//    Vc.tabBarItem.title = @"精华";
//    [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor],NSFontAttributeName:[UIFont systemFontOfSize:12]}forState:UIControlStateNormal];
//    [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]}forState:UIControlStateSelected];
//    Vc.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
//    UIImage *img = [UIImage imageNamed:@"tabBar_essence_click_icon"];
//    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    Vc.tabBarItem.selectedImage = img;
//    [self addChildViewController:Vc];
//    
//    UIViewController *Vc2 = [[UIViewController alloc]init];
//    Vc2.view.backgroundColor = [UIColor yellowColor];
//    Vc2.tabBarItem.title = @"最新";
//    [Vc2.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]}forState:UIControlStateNormal];
//    [Vc2.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]}forState:UIControlStateSelected];
//    Vc2.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
//    UIImage *img2 = [UIImage imageNamed:@"tabBar_new_click_icon"];
//    img2 = [img2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    Vc2.tabBarItem.selectedImage = img2;
//    [self addChildViewController:Vc2];
//
//    UIViewController *Vc3 = [[UIViewController alloc]init];
//    Vc3.view.backgroundColor = [UIColor blueColor];
//    Vc3.tabBarItem.title = @"关注";
//    [Vc3.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]}forState:UIControlStateNormal];
//    [Vc3.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]}forState:UIControlStateSelected];
//    Vc3.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
//    UIImage *img3 = [UIImage imageNamed:@"tabBar_friendTrends_click_icon"];
//    img3 = [img3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    Vc3.tabBarItem.selectedImage = img3;
//    [self addChildViewController:Vc3];
//
//    UIViewController *Vc4 = [[UIViewController alloc]init];
//    Vc4.view.backgroundColor = [UIColor redColor];
//    Vc4.tabBarItem.title = @"我";
//    [Vc4.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor]}forState:UIControlStateNormal];
//    [Vc4.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]}forState:UIControlStateSelected];
//    Vc4.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
//    UIImage *img4 = [UIImage imageNamed:@"tabBar_me_click_icon"];
//    img4 = [img4 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    Vc4.tabBarItem.selectedImage = img4;
//    [self addChildViewController:Vc4];
    
//    XHLog(@"%@",self.tabBar.subviews);
}

//-(void)viewDidAppear:(BOOL)animated{
//    [super viewDidAppear:animated];
//    XHLog(@"%@",self.tabBar.subviews);
//    UIButton *button = [UIButton buttonWithType:UIButtonTypeContactAdd];
//    button.center = CGPointMake(XHScreenW * 0.5, self.tabBar.frame.size.height * 0.5);
//    [self.tabBar addSubview:button];
//    //通过 bringSubviewToFront 方法可以让按钮显示在最上层
//    [self.view bringSubviewToFront:button];
//}

-(void)setupChildVc:(UIViewController *)Vc Title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
    
//    Vc.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(256) / 255.0 green:arc4random_uniform(256) / 255.0 blue:arc4random_uniform(256) / 255.0 alpha:1.0];
    //设置标题
    Vc.tabBarItem.title = title;
//    [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor grayColor],NSFontAttributeName:[UIFont systemFontOfSize:12]}forState:UIControlStateNormal];
//    [Vc.tabBarItem setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor darkGrayColor]}forState:UIControlStateSelected];
    //默认图片
    Vc.tabBarItem.image = [UIImage imageNamed:image];
    //选中图片
    UIImage *img = [UIImage imageNamed:selectedImage];
    //设置图片的渲染模式 --> (也可以在图片处设置)
//    img = [img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    Vc.tabBarItem.selectedImage = img;
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:Vc];
    //设置导航栏的背景图片
    [nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
    [self addChildViewController:nav];
}


@end
